package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.admin.pojo.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealService;
import com.itheima.util.RequestParesUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @Author longbosheng
 * @Date 2022/6/18 9:59
 * @PackageName:com.itheima.controller
 * @ClassName: SetmealController
 * @Description: TODO
 * @Version 1.0
 */
@RestController
@RequestMapping("/setmeal")
@Log4j2
public class SetmealController {
    @Autowired
    private SetmealService setmealService;


    /**
     * 套餐添加
     * @param setmeal
     * @param request
     * @return
     */
    @PostMapping
    public R addDish(@RequestBody Setmeal setmeal , HttpServletRequest request) {
        String id = RequestParesUtil.RequestPares(request);
        setmeal.setType("1");
        setmeal.setCreateUser(id);
        setmeal.setCreateTime(new Date());
        setmeal.setUpdateTime(new Date());
        setmeal.setUpdateUser(id);
        return setmealService.addDish(setmeal);
    }

    /**
     * 套餐分页查询
     *
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R showSetmeal(String name, int page, int pageSize) {
        return setmealService.selectSetmeal(name,page,pageSize);
    }

    /**
     * 修改套餐
     * @param setmeal
     * @return
     */
    @PutMapping
    public R updateSetmeal(@RequestBody Setmeal setmeal, HttpServletRequest request){
        String id = RequestParesUtil.RequestPares(request);
        setmeal.setUpdateUser(id);
        setmeal.setUpdateTime(new Date());
        return setmealService.updateSetmeal(setmeal);
    }

    /**
     * 按id查询套餐信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R selectDishById(@PathVariable String id){
        return setmealService.selectSetmealById(id);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R deleteDish(@RequestParam List<String> ids){
        return setmealService.deleteSetmeal(ids);
    }

    /**
     * 批量上架/下架
     * @param
     * @return
     */
    @PostMapping("/status/{status}{ids}")
    public R updateStatus(@PathVariable int status,@RequestParam List<String> ids,HttpServletRequest request){
        String updateUser = RequestParesUtil.RequestPares(request);
        System.out.println("status:"+status);
        System.out.println("id列表："+ids);
        return setmealService.updateSetmealStatus(status,ids,updateUser);
    }
}
